package it.rossignoli.enrico.app.Tesi;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class SelectionEvent extends Activity {
	
	private ListView lView;
	
	private static final String LOG_TAG="SelectionEvent";
	
	private ArrayList<String> itemsListView;
	private String title;
	
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.sel);
	        Intent intent=getIntent();
	        title=intent.getStringExtra("SMARTOBJECT_GESTURE");
	        setTitle(title);
	        
	        itemsListView=intent.getStringArrayListExtra("SMARTOBJECT_EVENTLISTNOTSELECTED");
    
	        lView = (ListView) findViewById(R.id.list);
	 }
	 
	 public void onResume()
	 {
		 super.onResume();
//	   	 Set option as Multiple Choice. So that user can able to select more the one option from list
		 ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, 
				 android.R.layout.simple_list_item_single_choice,itemsListView);
		 lView.setAdapter(adapter);
		 adapter.notifyDataSetChanged();
		 lView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		 lView.setItemChecked(itemsListView.size()-1, true);
	 }
	 
	 
	public void confirmButton(View v)
	{
		String selection=(String)lView.getItemAtPosition(lView.getCheckedItemPosition());
		if(SibIntentService.DEBUG) Log.i(LOG_TAG,"Gesture selected "+title);
		if(SibIntentService.DEBUG) Log.i(LOG_TAG,"Item selected "+selection);
//		Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
		if(!selection.equals(MappingActivity.NULL_ELEMENT))
		{
			itemsListView.remove(selection);
		}
		
		for(String s:itemsListView)
		{
			if(SibIntentService.DEBUG) Log.i(LOG_TAG, s);
		}
		
		Intent i=new Intent(SelectionEvent.this, MappingActivity.class);
		i.putExtra("RESPONSE_MAPPING_SELECTION", true);
		i.putExtra("SMARTOBJECT_GESTURE", title);
		i.putExtra("DEVICE_EVENT", selection);
		i.putExtra("SMARTOBJECT_EVENTLISTNOTSELECTED", itemsListView);
		setResult(Activity.RESULT_OK, i);
		finish();
	 }
	
	@Override
	public void onBackPressed() {
	   finish();
	   super.onBackPressed();
	}
}
